package com.iris.live.services.report.data.orgline;

import com.iris.live.services.common.JsonObjectUtils;
import com.iris.live.services.common.StringUtils;
import com.iris.live.services.data.models.ReportConfig;
import com.iris.live.services.report.data.ReportDataConvertor;
import com.iris.live.services.report.data.formulars.FormularFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:按照组织架构数据转换
 * @author: Tony.Zhang
 * @Date: 2017-08-11 12:06
 **/
@Component("OrglineConvertor")
public class OrglineConvertor  extends ReportDataConvertor<OrglineContext> {
    private Logger logger = LoggerFactory.getLogger(OrglineConvertor.class);

    @Override
    protected void getFormularData(OrglineContext context, Map<String, Map> datas, Map reportRow, ReportConfig config) throws Exception {
        String fullPath = this.getFullPath(config), formularExpression = config.getFormularExpression();
        Map resultCell = null;
        String formular = formularExpression.substring(0, formularExpression.indexOf("("));
        List<OrgUnit> units = context.getOrgUnits();
        for(OrgUnit unit : units){ //按照用户设置的时间轴上的时间和当前时间进行比较并进行合计
            resultCell = (Map) reportRow.get(fullPath + "_" + unit.getValue());
            if (resultCell == null) {
                resultCell = new HashMap<>();
                resultCell.put("color", 0);
                resultCell.put("displayRow", unit.getText());
                resultCell.put("displayRowOrder", unit.getDisplayRowOrder());
                resultCell.put(config.getCategory() + "Value", 0);
            }

            Object value = getData(formular, config, unit, datas);
            this.setResultCell(resultCell, config, value);
            reportRow.put(fullPath + "_" +unit.getValue(), resultCell);
        }
    }

    /**
     * 从数据库获取报表数据
     * @param resultSet
     * @param config
     * @return
     * @throws Exception
     */
    protected void getData(OrglineContext context, Map reportRow, Map resultSet, ReportConfig config) throws Exception{
        Map resultCell = null;
        String valueKey = config.getValueKey(), fullPath = this.getFullPath(config);
        Object value = null;

        List<OrgUnit> units = context.getOrgUnits();
        if(config.isBlank()) {
            for (OrgUnit unit : units) {
                resultCell = (Map) reportRow.get(fullPath + "_" + unit.getValue());
                if (resultCell == null) {
                    resultCell = new HashMap<>();
                    resultCell.put("color", 0);
                    resultCell.put("displayRow", unit.getText());
                    resultCell.put("displayRowOrder", unit.getDisplayRowOrder());
                    resultCell.put(config.getCategory() + "Value", null);
                }
                reportRow.put(fullPath + "_" + unit.getValue(), resultCell);
            }
        }

        if(! resultSet.containsKey(valueKey)){
            resultCell = new HashMap();
            resultCell.put("color", 0);
            this.setResultCell(resultCell, config, 0);

            return;
        }

        if(resultSet.get(valueKey) != null){
            value = resultSet.get(valueKey);
        }else{
            value = 0;
        }

        //处理累计
        for (OrgUnit unit : units) {
            resultCell = (Map) reportRow.get(fullPath + "_" +unit.getValue());
            if (resultCell == null) {
                resultCell = new HashMap<>();
                resultCell.put("color", 0);
                resultCell.put("displayRow", unit.getText());
                resultCell.put("displayRowOrder", unit.getDisplayRowOrder());
                resultCell.put(config.getCategory()+ "Value",Double.valueOf(0));
            }
            this.setResultCell(resultCell, config, 0);
            reportRow.put(fullPath + "_" +unit.getValue() , resultCell);
        }

        String reportKeyValue = null;
        for (OrgUnit unit : units) { //按照用户设置的组织架构轴上的组织单元进行合计
            switch (unit.getType()) {
                case GROUP: //按集团合计
                    String headerGroupValue = unit.getText(),
                            recordGroupValue = resultSet.get(unit.getOrgColumn()).toString();

                    reportKeyValue =  unit.getValue();
                    if (headerGroupValue.equals(recordGroupValue)) {
                        resultCell = (Map) reportRow.get(fullPath + "_" +reportKeyValue);
                        if (resultCell == null) {
                            resultCell = new HashMap<>();
                            resultCell.put("color", 0);
                            resultCell.put("displayRow", headerGroupValue);
                            resultCell.put("displayRowOrder", unit.getDisplayRowOrder());
                        }
                        this.setResultCell(resultCell, config, value);
                        reportRow.put(fullPath + "_" +reportKeyValue , resultCell);
                    }
                    break;
                case BIGAREA: //按大区合计
                    String headerBigAreaValue = unit.getText(),
                           recordBigAreaValue = resultSet.get(unit.getOrgColumn()).toString();
                    reportKeyValue =  unit.getValue();
                    if (headerBigAreaValue.equals(recordBigAreaValue)) {
                        resultCell = (Map) reportRow.get(fullPath + "_" +reportKeyValue);
                        if (resultCell == null) {
                            resultCell = new HashMap<>();
                            resultCell.put("color", 0);
                            resultCell.put("displayRow", headerBigAreaValue);
                            resultCell.put("displayRowOrder", unit.getDisplayRowOrder());
                        }
                        this.setResultCell(resultCell, config, value);
                        reportRow.put(fullPath + "_" +reportKeyValue , resultCell);
                    }
                    break;
                case SMALLAREA: //按小区合计
                    String  headerSmallAreaValue = unit.getText(),
                            recordSmallAreaValue = resultSet.get(unit.getOrgColumn()).toString();
                    reportKeyValue =  unit.getValue();
                    if (headerSmallAreaValue.equals(recordSmallAreaValue)) {
                        resultCell = (Map) reportRow.get(fullPath + "_" +reportKeyValue);
                        if (resultCell == null) {
                            resultCell = new HashMap<>();
                            resultCell.put("color", 0);
                            resultCell.put("displayRow", headerSmallAreaValue);
                            resultCell.put("displayRowOrder", unit.getDisplayRowOrder());
                        }
                        this.setResultCell(resultCell, config, value);
                        reportRow.put(fullPath + "_" +reportKeyValue , resultCell);
                    }
                    break;
                case CTA: //按CTA合计
                    String headerCTAValue = unit.getText(),
                           recordCTAValue = resultSet.get(unit.getOrgColumn()).toString();
                    reportKeyValue =  unit.getValue();
                    if (headerCTAValue.equals(recordCTAValue)) {
                        resultCell = (Map) reportRow.get(fullPath + "_" +reportKeyValue);
                        if (resultCell == null) {
                            resultCell = new HashMap<>();
                            resultCell.put("color", 0);
                            resultCell.put("displayRow", headerCTAValue);
                            resultCell.put("displayRowOrder", unit.getDisplayRowOrder());
                        }
                        this.setResultCell(resultCell, config, value);
                        reportRow.put(fullPath + "_" +reportKeyValue, resultCell);
                    }
                    break;
                default: //按许可合计
                    String headerPermitValue = unit.getText(),
                            recordPermitValue = resultSet.get(unit.getOrgColumn()).toString();
                    reportKeyValue =  unit.getValue();
                    if (headerPermitValue.equals(recordPermitValue)) {
                        resultCell = (Map) reportRow.get(fullPath + "_" +reportKeyValue);
                        if (resultCell == null) {
                            resultCell = new HashMap<>();
                            resultCell.put("color", 0);
                            resultCell.put("displayRow", headerPermitValue);
                            resultCell.put("displayRowOrder", unit.getDisplayRowOrder());
                        }
                        this.setResultCell(resultCell, config, value);
                        reportRow.put(fullPath + "_" +reportKeyValue , resultCell);
                    }
            }
        }
    }

    /**
     * 获取公式项值
     * @param formular
     * @param config
     * @param unit
     * @param datas
     * @return
     * @throws Exception
     */
    protected Double getData(String formular, ReportConfig config, OrgUnit unit, Map<String, Map> datas) throws Exception{
        return  FormularFactory.getFormular(formular).getValue(config, unit.getValue(), datas);
    }
}